package com.fhminyi.GUI;

import com.fhminyi.dao.UserDao;
import com.fhminyi.model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class UserProfileFrame extends JFrame {
    private User currentUser;
    private JTextField usernameField;
    private JTextField emailField;
    private JTextField phoneField;
    private JTextField addressField;
    private JTextField userIdField; // 添加用户ID字段
    private JTextField roleField; // 添加用户角色字段
    private JPasswordField passwordField; // 添加密码字段

    public UserProfileFrame(User user) {
        this.currentUser = user;
        setTitle("个人信息");
        setSize(500, 450); // 调整窗口大小以适应新增的密码字段
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 设置边距
        mainPanel.setBackground(new Color(245, 245, 245)); // 设置背景色

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 设置组件间距
        gbc.anchor = GridBagConstraints.WEST; // 左对齐

        // 用户ID
        JLabel userIdLabel = new JLabel("用户ID:");
        userIdLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(userIdLabel, gbc);

        userIdField = new JTextField(String.valueOf(user.getUserId()), 20); // 修复用户ID显示问题
        userIdField.setEditable(false); // 用户ID不可编辑
        userIdField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        userIdField.setBackground(new Color(230, 230, 230)); // 设置背景色
        gbc.gridx = 1;
        gbc.gridy = 0;
        mainPanel.add(userIdField, gbc);

        // 用户角色
        JLabel roleLabel = new JLabel("用户权限:");
        roleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 1;
        mainPanel.add(roleLabel, gbc);

        roleField = new JTextField(user.getRole(), 20);
        roleField.setEditable(false); // 用户角色不可编辑
        roleField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        roleField.setBackground(new Color(230, 230, 230)); // 设置背景色
        gbc.gridx = 1;
        gbc.gridy = 1;
        mainPanel.add(roleField, gbc);

        // 用户名
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 2;
        mainPanel.add(usernameLabel, gbc);

        usernameField = new JTextField(user.getUsername(), 20);
        usernameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 2;
        mainPanel.add(usernameField, gbc);

        // 邮箱
        JLabel emailLabel = new JLabel("邮箱:");
        emailLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 3;
        mainPanel.add(emailLabel, gbc);

        emailField = new JTextField(user.getEmail(), 20);
        emailField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 3;
        mainPanel.add(emailField, gbc);

        // 电话
        JLabel phoneLabel = new JLabel("电话:");
        phoneLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 4;
        mainPanel.add(phoneLabel, gbc);

        phoneField = new JTextField(user.getPhone(), 20);
        phoneField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 4;
        mainPanel.add(phoneField, gbc);

        // 地址
        JLabel addressLabel = new JLabel("地址:");
        addressLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 5;
        mainPanel.add(addressLabel, gbc);

        addressField = new JTextField(user.getAddress(), 20);
        addressField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 5;
        mainPanel.add(addressField, gbc);

        // 密码
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        gbc.gridx = 0;
        gbc.gridy = 6;
        mainPanel.add(passwordLabel, gbc);

        passwordField = new JPasswordField(20); // 添加密码字段
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField.setText(user.getPassword()); // 初始化密码字段
        gbc.gridx = 1;
        gbc.gridy = 6;
        mainPanel.add(passwordField, gbc);

        // 保存按钮
        JButton saveButton = new JButton("保存");
        saveButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        saveButton.setBackground(new Color(0, 102, 204)); // 设置按钮背景色
        saveButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        saveButton.setFocusPainted(false); // 去除焦点边框
        saveButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); // 设置按钮内边距
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveUserInfo();
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER; // 居中对齐
        mainPanel.add(saveButton, gbc);

        add(mainPanel);
    }

    private void saveUserInfo() {
        // 更新用户信息
        currentUser.setEmail(emailField.getText());
        currentUser.setPhone(phoneField.getText());
        currentUser.setAddress(addressField.getText());
        currentUser.setPassword(new String(passwordField.getPassword())); // 更新密码

        // 保存用户信息到数据库
        UserDao userDao = new UserDao();
        try {
            userDao.updateUser(currentUser);
            JOptionPane.showMessageDialog(this, "个人信息已保存", "成功", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "保存个人信息失败", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 测试
        User user = new User();
        user.setUserId(1);
        user.setUsername("testUser");
        user.setEmail("test@example.com");
        user.setPhone("1234567890");
        user.setAddress("测试地址");
        user.setRole("普通用户");
        user.setPassword("password123"); // 设置初始密码

        SwingUtilities.invokeLater(() -> {
            new UserProfileFrame(user).setVisible(true);
        });
    }
}